/*
 *  Copyright 2015 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

package com.cloudptt.wp.ui.call.single;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.single.LaunchCallInfo;
import com.cloudptt.wp.R;

/**
 * Fragment for call control.
 */
public class CallFragment extends Fragment implements View.OnClickListener{
	public static String THIS_FILE = "CallFragment";
	private View controlView;
	private LinearLayout controlLy = null;
	private LinearLayout callControlLy = null;
//	private ImageButton acceptButton;
//	private ImageButton disconnectButton;
//	private ImageButton cameraSwitchButton;
//	private ImageButton videoScalingButton;
//	private ImageButton enableMicIb;
//	private ImageButton enableSpeakerIb;
//	private TextView captureFormatText;
//	private SeekBar captureFormatSlider;
	private OnCallEvents callEvents;
//	private ScalingType scalingType;
//	private int callType = CallAttr.CallType_Private;
	private boolean videoCallEnabled = true;
//	//  private WebRtcParameters para = null;
//	private boolean isCalled = false;
//	private int callState = CallConst.CallState_InitState;
//	private boolean isMicEnabled = true;
	private LaunchCallInfo callInfo = null;
	private RelativeLayout switchLy = null;
	private ImageButton switchBtn = null;
	private RelativeLayout acceptLy;
	private RelativeLayout disconnectLy;

	private RelativeLayout micLy;
	private ImageButton micImageBtn = null;
	private TextView micTv = null;

	private RelativeLayout speakerLy;
	private ImageButton speakerImageBtn = null;
	private TextView speakerTv = null;
//	private RelativeLayout mCameraRLayout;
//	private RelativeLayout mVideoScalingRLayout;
//	private RelativeLayout mToggleMuteRLayout;
	private int curCallState = CallConst.CallState_InitState;
	private boolean enableMic = true;
	private boolean enableSpeaker = false;
	private boolean isFrontCamera = true;

	public CallFragment(LaunchCallInfo callInfo)
	{
		this.callInfo = callInfo;
	}

	/**
	 * Call control interface for container activity.
	 */
	public interface OnCallEvents {
//		public void acceptCall();
//		public void hangupCall();
//		public void onCameraSwitch();
//		public void onVideoScalingSwitch(ScalingType scalingType);
//		public void onCaptureFormatChange(int width, int height, int framerate);
		public void doUserAction(int btnAction,String infoStr);
		public boolean setSpeakerphoneEnable(boolean isOn);
		public boolean setMicrophoneEnable(boolean isOn);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
		Log.d(THIS_FILE, "onCreateView");
		controlView =
				inflater.inflate(R.layout.fragment_call, container, false);

		// Create UI controls.
		controlLy =
				(LinearLayout) controlView.findViewById(R.id.controlLy);
		callControlLy=
				(LinearLayout) controlView.findViewById(R.id.callControlLy);
		switchLy =
				(RelativeLayout) controlView.findViewById(R.id.switchLy);
		switchBtn =
				(ImageButton) controlView.findViewById(R.id.switchBtn);
		switchBtn.setOnClickListener(this);

		acceptLy =
				(RelativeLayout) controlView.findViewById(R.id.acceptLy);
		acceptLy.setOnClickListener(this);
		ImageButton acceptBtn =
				(ImageButton) controlView.findViewById(R.id.acceptBtn);
		acceptBtn.setOnClickListener(this);
		disconnectLy =
				(RelativeLayout) controlView.findViewById(R.id.disconnectLy);
		disconnectLy.setOnClickListener(this);
		ImageButton disconnectBtn =
				(ImageButton) controlView.findViewById(R.id.disconnectBtn);
		disconnectBtn.setOnClickListener(this);

		micLy =
				(RelativeLayout) controlView.findViewById(R.id.micLy);
		micLy.setOnClickListener(this);
		micImageBtn =
				(ImageButton) controlView.findViewById(R.id.micImageBtn);
		micImageBtn.setOnClickListener(this);
		micTv =
				(TextView) controlView.findViewById(R.id.micTv);
		micTv.setOnClickListener(this);
		micLy.setVisibility(View.GONE);
		enableMicImageBtn(enableMic);

		speakerLy = (RelativeLayout) controlView.findViewById(R.id.speakerLy);
		speakerLy.setOnClickListener(this);
		speakerImageBtn =
				(ImageButton) controlView.findViewById(R.id.speakerImageBtn);
		speakerImageBtn.setOnClickListener(this);
		speakerTv =
				(TextView) controlView.findViewById(R.id.speakerTv);
		speakerTv.setOnClickListener(this);
		speakerLy.setVisibility(View.GONE);
		enableSpeakerImageBtn(enableSpeaker);

//		disconnectButton =
//				(ImageButton) controlView.findViewById(R.id.button_call_disconnect);
//		mCameraRLayout =
//				(RelativeLayout) controlView.findViewById(R.id.rl_switch_camera);
//		cameraSwitchButton =
//				(ImageButton) controlView.findViewById(R.id.button_call_switch_camera);
//		if(callInfo != null
//				&& callInfo.isHasVideo())
//		{
//			cameraSwitchButton.setVisibility(View.VISIBLE);
//		}
//		else
//		{
//			cameraSwitchButton.setVisibility(View.INVISIBLE);
//		}
//
//		enableMicIb =
//				(ImageButton) controlView.findViewById(R.id.enableMicIb);
//		enableMicIb.setEnabled(enableMic);
//		enableMicIb.setVisibility(View.INVISIBLE);

//		enableSpeakerIb =
//				(ImageButton) controlView.findViewById(R.id.enableSpeakerIb);
//		enableSpeakerIb.setEnabled(enableSpeaker);

		// Add buttons click events.
//		acceptLy.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				callEvents.acceptCall();
//			}
//		});
		//    if(isCalled)
		//    {
		//    	disconnectButton.setEnabled(false);
		//    }
//		disconnectLy.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				callEvents.hangupCall();
//			}
//		});
//		cameraSwitchButton.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				callEvents.onCameraSwitch();
//			}
//		});
//		videoScalingButton.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				if (scalingType == ScalingType.SCALE_ASPECT_FILL) {
//					videoScalingButton.setBackgroundResource(
//							R.drawable.ic_action_full_screen);
//					scalingType = ScalingType.SCALE_ASPECT_FIT;
//				} else {
//					videoScalingButton.setBackgroundResource(
//							R.drawable.ic_action_return_from_full_screen);
//					scalingType = ScalingType.SCALE_ASPECT_FILL;
//				}
//				callEvents.onVideoScalingSwitch(scalingType);
//			}
//		});
//		scalingType = ScalingType.SCALE_ASPECT_FILL;
//		int callType = this.callInfo.getCallType();
//		if(callType == CallAttr.CallType_Webrtc_Single)
//		{
////			boolean isMicEnabled = this.callInfo.isMicEnabled();
////			toggleMuteButton.setSelected(!isMicEnabled);
//			boolean enabled = callEvents.isMicEnable();
//			toggleMuteButton.setSelected(!enabled);
//		}
//		else
//		{
//			toggleMuteButton.setEnabled(false);
//		}
		
//		enableMicIb.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				enableMic = !enableMic;
//				enableMic = callEvents.setMicrophoneEnable(enableMic);
//				enableMicIb.setSelected(!enableMic);
//			}
//		});

		return controlView;
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.d(THIS_FILE, "onStart callInfo = " + callInfo);
		boolean captureSliderEnabled = false;
		//    String contactName = args.getString(CallActivity.EXTRA_IP);
		//    contactView.setText(contactName);
		//    videoCallEnabled = args.getBoolean(CallActivity.EXTRA_VIDEO_CALL, true);
		//    videoCallEnabled = para.isVideoCallEnabled();
		captureSliderEnabled = false;//videoCallEnabled && para.isCaptureQualitySlider();

		
//		if (!videoCallEnabled) {
//			cameraSwitchButton.setVisibility(View.INVISIBLE);
//		}
		if (captureSliderEnabled) {
//			captureFormatSlider.setOnSeekBarChangeListener(
//					new CaptureQualityController(captureFormatText, callEvents));
		}

		int callState = callInfo.getCallState();
		Log.d(THIS_FILE, "onStart callState = " + callState);
		updateView(callState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		callEvents = (OnCallEvents) activity;
	}

	@Override
	public void onClick(View v) {
		Log.debug(this.getClass(), "onClick v.getId() = " + v.getId());

		v.setEnabled(false); // 立即禁用按钮
		// 500ms 后恢复可用
		v.postDelayed(() -> v.setEnabled(true), 500);

		if (R.id.acceptLy == v.getId()
				|| R.id.acceptBtn == v.getId()) {
//			callEvents.acceptCall();
			callEvents.doUserAction(CallConst.BtnAction_Answer,"");
		} else if (R.id.disconnectLy == v.getId()
				|| R.id.disconnectBtn == v.getId()) {
//			callEvents.hangupCall();
			callEvents.doUserAction(CallConst.BtnAction_Hangup,"");
		}
		else if (R.id.micLy == v.getId()
				|| R.id.micImageBtn == v.getId()) {
			Log.debug(this.getClass(), "onClick begin enableMic = " + enableMic);
			enableMic = callEvents.setMicrophoneEnable(!enableMic);
			Log.debug(this.getClass(), "onClick end enableMic = " + enableMic);
			enableMicImageBtn(enableMic);
		}
		else if (R.id.speakerLy == v.getId()
				|| R.id.speakerImageBtn == v.getId()) {
			Log.debug(this.getClass(), "onClick begin enableSpeaker = " + enableSpeaker);
			enableSpeaker = !enableSpeaker;
			callEvents.setSpeakerphoneEnable(enableSpeaker);
			Log.debug(this.getClass(), "onClick end enableSpeaker = " + enableSpeaker);
			enableSpeakerImageBtn(enableSpeaker);
		}
		else if(R.id.switchBtn == v.getId())
		{
			isFrontCamera = !isFrontCamera;
			if(isFrontCamera)
			{
				switchBtn.setImageResource(R.drawable.btn_group_call_video_camera_single_normal);
			}
			else
			{
				switchBtn.setImageResource(R.drawable.btn_group_call_video_camera_single_pressed);
			}
//			callEvents.onCameraSwitch();
			callEvents.doUserAction(CallConst.BtnAction_SwitchCamera,"");
		}
	}

	public void  enableMicImageBtn(boolean isEnable)
	{
		if(isEnable)
		{
			micTv.setText(R.string.call_btn_mic_on);
			micImageBtn.setImageResource(R.drawable.btn_call_mic_normal);
		}
		else
		{
			micTv.setText(R.string.call_btn_mic_off);
			micImageBtn.setImageResource(R.drawable.btn_call_mic_pressed);
		}
	}
	public void enableSpeakerImageBtn(boolean isEnable)
	{
		if(enableSpeaker)
		{
			speakerTv.setText(R.string.call_btn_speaker_on);
			speakerImageBtn.setImageResource(R.drawable.btn_call_speaker_normal);
		}
		else
		{
			speakerTv.setText(R.string.call_btn_speaker_off);
			speakerImageBtn.setImageResource(R.drawable.btn_call_speaker_pressed);
		}
	}

	public void updateView(int callState)
	{
		if(callState == CallConst.CallState_Activite)
		{
			this.viewOnConnected();
		}
		else
		{
			if(callInfo.isMoCall())
			{
				viewOnCalling();
			}
			else
			{
//				viewOnIncoming();
				this.viewOnConnected();
			}
		}
	}

//	public void viewAfterOffer()
//	{
//		disconnectButton.setEnabled(true);
//	}
	private void viewOnCalling()
	{
		//acceptButton.setVisibility(View.GONE);
		//disconnectButton.setVisibility(View.VISIBLE);
		//cameraSwitchButton.setVisibility(View.GONE);
		//toggleMuteButton.setVisibility(View.GONE);

		if(acceptLy != null)
		{
		acceptLy.setVisibility(View.GONE);
		}
		disconnectLy.setVisibility(View.VISIBLE);
//		mCameraRLayout.setVisibility(View.GONE);
//		mToggleMuteRLayout.setVisibility(View.GONE);

	}

	private void viewOnIncoming()
	{
		/*acceptButton.setVisibility(View.VISIBLE);
	  disconnectButton.setVisibility(View.VISIBLE);
	  cameraSwitchButton.setVisibility(View.GONE);
	  toggleMuteButton.setVisibility(View.GONE);*/
		if(acceptLy != null) {
			acceptLy.setVisibility(View.VISIBLE);
		}
		disconnectLy.setVisibility(View.VISIBLE);
//		mCameraRLayout.setVisibility(View.GONE);
//		mToggleMuteRLayout.setVisibility(View.GONE);
	}

	public void viewOnConnected()
	{
		/*acceptButton.setVisibility(View.GONE);
	  disconnectButton.setVisibility(View.VISIBLE);
	  cameraSwitchButton.setVisibility(View.VISIBLE);
	  toggleMuteButton.setVisibility(View.VISIBLE);*/

		if(acceptLy != null) {
			acceptLy.setVisibility(View.GONE);
		}
		disconnectLy.setVisibility(View.VISIBLE);
		micLy.setVisibility(View.VISIBLE);
		speakerLy.setVisibility(View.VISIBLE);
		Log.d(THIS_FILE, "viewOnConnected callInfo = " + callInfo);
		if(callInfo != null)
		{
			boolean isMoCall = callInfo.isMoCall();
			int callType = callInfo.getCallType();
			Log.d(THIS_FILE, "viewOnConnected isMoCall = " + isMoCall + " callType = " + callType);
			if(isMoCall
					&& (callType == CallAttr.CallType_Pull_One_To_One || callType == CallAttr.CallType_Pull_One_To_Svr))
			{
				switchLy.setVisibility(View.GONE);
			}
			else if(!isMoCall
					&& (callType == CallAttr.CallType_Push_One_To_One || callType == CallAttr.CallType_Push_One_To_Svr))
			{
				switchLy.setVisibility(View.GONE);
			}
			else
			{
				boolean hasVideo = callInfo.isHasVideo();
				if(hasVideo)
				{
					switchLy.setVisibility(View.VISIBLE);
//				switchLy.setVisibility(View.GONE);
				}
			}
		}
//		mCameraRLayout.setVisibility(View.VISIBLE);
//		mToggleMuteRLayout.setVisibility(View.VISIBLE);
	}
	
	public void setCallState(LaunchCallInfo callInfo) {
		Log.d(THIS_FILE, "setCallState callInfo = " + callInfo);
		if(callInfo != null)
		{
			this.callInfo = callInfo;
			int callState = callInfo.getCallState();
			Log.d(THIS_FILE, "setCallState callState = " + callState + " curCallState = " + curCallState);
			if(curCallState <= callState) {
				curCallState = callState;
				updateView(callState);
			}
		}
	}

	public void hideControlUi(boolean isShow)
	{
		if(!callInfo.isHasVideo())
		{
			return;
		}
		if(isShow)
		{
//			recvInfoLy.setVisibility(View.VISIBLE);
			controlLy.setVisibility(View.VISIBLE);
			callControlLy.setVisibility(View.VISIBLE);
		}
		else {
//			recvInfoLy.setVisibility(View.INVISIBLE);
			controlLy.setVisibility(View.INVISIBLE);
			callControlLy.setVisibility(View.INVISIBLE);
		}
	}
}
